package name.weily.liba.views

import android.content.Context
import android.util.AttributeSet
import android.view.ContextMenu
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class RecyclerViewContextInfo : ContextMenu.ContextMenuInfo {
    var position: Int? = null
}

class ContextMenuRecyclerView(context: Context, attributeSet: AttributeSet?, defStyle: Int)
    : RecyclerView(context, attributeSet, defStyle) {

    private val recyclerViewContextInfo: RecyclerViewContextInfo by lazy { RecyclerViewContextInfo() }

    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    override fun showContextMenuForChild(originalView: View): Boolean {
        layoutManager?.apply {
            recyclerViewContextInfo.position = getPosition(originalView)
        }
        return super.showContextMenuForChild(originalView)
    }

    override fun showContextMenuForChild(originalView: View, x: Float, y: Float): Boolean {
        layoutManager?.apply {
            recyclerViewContextInfo.position = getPosition(originalView)
        }
        return super.showContextMenuForChild(originalView, x, y)
    }

    override fun getContextMenuInfo(): ContextMenu.ContextMenuInfo {
        return recyclerViewContextInfo
    }
}